package Views;

import Controllers.ItemCTR;
import Models.*;
import Views.TableModels.ItemTableModel;
import byggemarked.Tools;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Date;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @author Morten
 */
public class ItemPicker extends javax.swing.JFrame implements ListSelectionListener, WindowListener
{
 	private Item chosenItem;
	private ItemCTR controller = new ItemCTR();
	private KitchenPartType partType = null;
	private Supplier supplierTemp = null;
	private SupplierPicker sp = null;

    public ItemPicker()
	{
        this.init();
    }

	public ItemPicker(KitchenPartType partType)
	{
		this.partType = partType;
		this.init();
	}

	private void init()
	{
		initComponents();
		itemTable.getSelectionModel().addListSelectionListener(this);
		itemTable.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent e){
				if (e.getClickCount() == 2)
				{
					if(itemTable.getSelectedRowCount() == 1)
					{
						chooseButtonActionPerformed(null);
					}
				}
			}
		 });
	}

	public Item getChosenItem()
	{
		return this.chosenItem;
	}

	public KitchenPartType getPartType()
	{
		return this.partType;
	}

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        locationBG = new javax.swing.ButtonGroup();
        nameLabel = new javax.swing.JLabel();
        nameTF = new javax.swing.JTextField();
        producentLabel = new javax.swing.JLabel();
        producentTF = new javax.swing.JTextField();
        supplierLabel = new javax.swing.JLabel();
        supplierTF = new javax.swing.JTextField();
        findSupplierButton = new javax.swing.JButton();
        showSupplierButton = new javax.swing.JButton();
        priceLabel = new javax.swing.JLabel();
        priceSP = new javax.swing.JSpinner();
        priceKrLabel = new javax.swing.JLabel();
        aqusitionDateLabel = new javax.swing.JLabel();
        aqusitionDateTF = new javax.swing.JTextField();
        lumberYardRB = new javax.swing.JRadioButton();
        hardwareStoreRB = new javax.swing.JRadioButton();
        stockLabel = new javax.swing.JLabel();
        minStockLabel = new javax.swing.JLabel();
        minStockSP = new javax.swing.JSpinner();
        maxStockLabel = new javax.swing.JLabel();
        maxStockSP = new javax.swing.JSpinner();
        searchButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        itemTable = new javax.swing.JTable();
        chooseButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Varevælger");

        nameLabel.setText("Navn:");

        producentLabel.setText("Producent:");

        supplierLabel.setText("Leverandør:");

        supplierTF.setEnabled(false);

        findSupplierButton.setText("Find");
        findSupplierButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findSupplierButtonActionPerformed(evt);
            }
        });

        showSupplierButton.setText("Info");
        showSupplierButton.setEnabled(false);
        showSupplierButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showSupplierButtonActionPerformed(evt);
            }
        });

        priceLabel.setText("Pris:");

        priceKrLabel.setText("kr.");

        aqusitionDateLabel.setText("Købsdato:");

        locationBG.add(lumberYardRB);
        lumberYardRB.setText("Trælasthandel");

        locationBG.add(hardwareStoreRB);
        hardwareStoreRB.setText("Byggemarked");

        stockLabel.setText("Lager:");

        minStockLabel.setText("Min.");

        maxStockLabel.setText("Max.");

        searchButton.setText("Søg");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        itemTable.setModel(new ItemTableModel());
        jScrollPane2.setViewportView(itemTable);

        chooseButton.setText("Vælg");
        chooseButton.setEnabled(false);
        chooseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Annuller");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(lumberYardRB)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(hardwareStoreRB))
                    .add(layout.createSequentialGroup()
                        .add(minStockLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(minStockSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 47, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(maxStockLabel)
                        .add(10, 10, 10)
                        .add(maxStockSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 44, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(searchButton)
                    .add(stockLabel)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(nameLabel)
                            .add(producentLabel)
                            .add(supplierLabel)
                            .add(aqusitionDateLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(layout.createSequentialGroup()
                                .add(supplierTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 140, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(findSupplierButton)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(showSupplierButton))
                            .add(producentTF)
                            .add(nameTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
                            .add(aqusitionDateTF)
                            .add(layout.createSequentialGroup()
                                .add(priceSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 98, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(priceKrLabel))))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(chooseButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(cancelButton))
                    .add(priceLabel)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(nameLabel))
                .add(12, 12, 12)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(producentTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(producentLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(supplierTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(supplierLabel)
                    .add(findSupplierButton)
                    .add(showSupplierButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(priceLabel)
                    .add(priceSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(priceKrLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(aqusitionDateTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(aqusitionDateLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lumberYardRB)
                    .add(hardwareStoreRB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(stockLabel)
                .add(2, 2, 2)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(minStockLabel)
                        .add(minStockSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(maxStockLabel)
                        .add(maxStockSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(searchButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 94, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancelButton)
                    .add(chooseButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelButtonActionPerformed
	{//GEN-HEADEREND:event_cancelButtonActionPerformed
		this.dispose();
}//GEN-LAST:event_cancelButtonActionPerformed

	private void chooseButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_chooseButtonActionPerformed
	{//GEN-HEADEREND:event_chooseButtonActionPerformed
		int chosenId = Integer.parseInt(itemTable.getValueAt(itemTable.getSelectedRow(), 0).toString());
		this.chosenItem = this.controller.getItem(chosenId);
		this.doClose();
}//GEN-LAST:event_chooseButtonActionPerformed

	private void searchButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_searchButtonActionPerformed
	{//GEN-HEADEREND:event_searchButtonActionPerformed
		Location location = null;
		if(lumberYardRB.isSelected())
		{ location = Location.LumberYard; }
		else if(hardwareStoreRB.isSelected())
		{ location = Location.HardwareStore; }
		Date aqusitionDate = null;
		if(!aqusitionDateTF.getText().equals(""))
		{ aqusitionDate = Tools.makeDateFromString(aqusitionDateTF.getText()); }
		itemTable.setModel(
			new ItemTableModel(
				this.controller.findItem(nameTF.getText(),
										null,
										producentTF.getText(),
										this.supplierTemp,
										Double.parseDouble(priceSP.getValue().toString()),
										aqusitionDate,
										location,
										Integer.parseInt(minStockSP.getValue().toString()),
										Integer.parseInt(maxStockSP.getValue().toString()))));
	}//GEN-LAST:event_searchButtonActionPerformed

	private void findSupplierButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_findSupplierButtonActionPerformed
	{//GEN-HEADEREND:event_findSupplierButtonActionPerformed
		sp = new SupplierPicker();
		sp.addWindowListener(this);
		sp.setVisible(true);
	}//GEN-LAST:event_findSupplierButtonActionPerformed

	private void showSupplierButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_showSupplierButtonActionPerformed
	{//GEN-HEADEREND:event_showSupplierButtonActionPerformed
		new SupplierInfoView(this.supplierTemp).setVisible(true);
	}//GEN-LAST:event_showSupplierButtonActionPerformed

	public void valueChanged(ListSelectionEvent e)
	{
		if(itemTable.getSelectedRowCount() == 1)
		{
			chooseButton.setEnabled(true);
		}
		else
		{
			chooseButton.setEnabled(false);
		}
	}

	public void windowClosing(WindowEvent e)
	{
		if(sp != null)
		{
			this.supplierTemp = sp.getChosenSupplier();
			supplierTF.setText(this.supplierTemp.getName());
		}
	}

	public void doClose()
	{
		WindowEvent evt = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
		processWindowEvent(evt);
	}

	public void windowOpened(WindowEvent e) {}
	public void windowClosed(WindowEvent e) {}
	public void windowIconified(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e) {}
	public void windowActivated(WindowEvent e) {}
	public void windowDeactivated(WindowEvent e) {}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel aqusitionDateLabel;
    private javax.swing.JTextField aqusitionDateTF;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton chooseButton;
    private javax.swing.JButton findSupplierButton;
    private javax.swing.JRadioButton hardwareStoreRB;
    private javax.swing.JTable itemTable;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.ButtonGroup locationBG;
    private javax.swing.JRadioButton lumberYardRB;
    private javax.swing.JLabel maxStockLabel;
    private javax.swing.JSpinner maxStockSP;
    private javax.swing.JLabel minStockLabel;
    private javax.swing.JSpinner minStockSP;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTF;
    private javax.swing.JLabel priceKrLabel;
    private javax.swing.JLabel priceLabel;
    private javax.swing.JSpinner priceSP;
    private javax.swing.JLabel producentLabel;
    private javax.swing.JTextField producentTF;
    private javax.swing.JButton searchButton;
    private javax.swing.JButton showSupplierButton;
    private javax.swing.JLabel stockLabel;
    private javax.swing.JLabel supplierLabel;
    private javax.swing.JTextField supplierTF;
    // End of variables declaration//GEN-END:variables
}